<script setup name="name">
import { ref, reactive, watchEffect, defineEmits } from 'vue'
import {
  GetFinishedListAPI,
  GetTypeSelectAPI,
  GetAwmateriaListAPI,
  GetAccessoriesAPI,
  GetWasteAPI,
  GetHalfListAPI
} from '@/api/material/common'
const { proxy } = getCurrentInstance()

// 物料类别
// const { sys_material_type } = proxy.useDict('sys_material_type')
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  },
  dict: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '物料'
  },
  // 是否单选
  bdb: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset', 'materialTypeChange'])

const loading = ref(false)
const tableRef = ref(null)
const typeSelect = ref([])

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialState: 1,
  materialName: '', // 名称
  materialCode: '', // 编码
  materialType: '', // 物料类别
  type: '' // 类别
})

const handleSelect = ref({ materialId: '' })
const handleSelects = ref([])
const TableData = ref([])
const total = ref(0)

// 获取物料类别
const getMaterialType = async () => {
  query.materialType = ''
  const res = await GetTypeSelectAPI({ condingId: query.type })
  typeSelect.value = res.theDrop
  // query.materialType = typeSelect.value[0].value
  Get()
}

// 勾选数据
const handleSelectionChange = row => {
  handleSelect.value = row
}

// 勾选数据
const handleSelectionChanges = row => {
  handleSelects.value = row
}

// 行内选中时
const rowClickChange = row => {
  tableRef.value.toggleRowSelection(row)
}

// 是否禁用 保存
const d = computed(() => {
  if (props.bdb) {
    return handleSelect.value?.materialId ? false : true
  } else {
    return handleSelects.value.length > 0 ? false : true
  }
})

// 提交
const preservation = () => {
  if (props.bdb) {
    const Obj = handleSelect.value
    Obj.materialTypeDict = props.dict.find(item => item.value == query.type).label
    // 物料类别
    Obj.materialType = query.type
    // 物料类型
    Obj.materialstate = query.materialType

    emit('preservation', Obj)
  } else {
    emit('preservation', {
      type: query.materialType,
      materialType: query.type,
      list: handleSelects.value
    })
  }
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  if (props.dict.length < 0) return proxy.errorMsg('请传入类型数据')

  query.type = props.dict[0].value
  Get()
}

// 物料改变
const materialTypeChange = val => {
  getMaterialType()
  emit('materialTypeChange', val)
}

//
const Get = () => {
  if (props.dict.length < 0) return proxy.errorMsg('请传入类型数据')
  TableData.value = []
  handleSelect.value = {}
  if (query.type == 1) getList()
  if (query.type == 2) getList2()
  if (query.type == 3) getList3()
  if (query.type == 4) getList4()
  if (query.type == 5) getList5()
}
// 查询 原料
const getList = async () => {
  loading.value = true
  const res = await GetAwmateriaListAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 辅料
const getList2 = async () => {
  loading.value = true
  const res = await GetAccessoriesAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 半成品
const getList3 = async () => {
  loading.value = true
  const res = await GetHalfListAPI(query)
  console.log(res, 'res')
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 成品
const getList4 = async () => {
  loading.value = true
  const res = await GetFinishedListAPI(query)
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

// 查询 废料
const getList5 = async () => {
  loading.value = true
  const res = await GetWasteAPI(query)
  if (res.code !== 200) return
  total.value = res.total
  TableData.value = res.rows
  loading.value = false
}

const init = () => {
  if (props.dict.length <= 0) return proxy.errorMsg('请传入类型数据')

  getMaterialType()
}
watch(
  () => props.dict,
  val => {
    query.type = val[0].value
    if (!query.type) return
    init()
  },
  {
    deep: true,
    immediate: true
  }
)
</script>
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <p-dialog v-model="props.isShow" :isBtn="false" :title="`${props.title}信息`" @close="emit('cancel')" width="65%">
      <template #body>
        <div class="item">
          <el-form :inline="true" label-width="100px">
            <el-form-item :label="`${props.title}类别`">
              <el-select v-model="query.type" :placeholder="`请选择${props.title}类别`" @change="materialTypeChange">
                <el-option v-for="(item, index) in props.dict" :key="index" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item :label="`${props.title}类型`">
              <el-select clearable v-model="query.materialType" :placeholder="`请选择${props.title}类型`" @change="Get">
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in typeSelect" :key="index" />
              </el-select>
            </el-form-item>
            <el-form-item :label="`${props.title}编码`">
              <el-input clearable v-model.trim="query.materialCode" :placeholder="`请输入${props.title}编码`" />
            </el-form-item>
            <el-form-item :label="`${props.title}名称`">
              <el-input clearable v-model.trim="query.materialName" :placeholder="`请输入${props.title}名称`" />
            </el-form-item>
            <el-form-item label="规格型号">
              <el-input clearable v-model.trim="query.specType" placeholder="请输入规格型号" />
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="Get">查询列表</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </div>
        <el-divider />
        <el-table
          ref="tableRef"
          stripe
          border
          :loading="loading"
          :data="TableData"
          highlight-current-row
          @current-change="handleSelectionChange"
          @selection-change="handleSelectionChanges"
          @row-click="rowClickChange"
        >
          <el-table-column label="序号" width="80" type="index" />
          <el-table-column width="80" v-if="props.bdb">
            <template #default="scope">
              <el-radio-group v-model="handleSelect.materialId">
                <el-radio :label="scope.row.materialId" size="large">&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column v-else type="selection" width="55" align="center" fixed="left" />
          <el-table-column label="物料编码" prop="materialCode" width="auto" />
          <el-table-column label="物料名称" prop="materialName" width="auto" />
          <el-table-column label="规格型号" prop="specType" width="auto" />
        </el-table>

        <pagination
          :fixed="false"
          v-show="total > 0"
          :total="total"
          v-model:page="query.pageNum"
          v-model:limit="query.pageSize"
          @pagination="Get"
        />
        <!-- 按钮 -->
        <div>
          <el-row class="Btn">
            <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
            <el-button type="info" @click="emit('cancel')">取消</el-button>
          </el-row>
        </div>
      </template>
    </p-dialog>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
.item {
  display: flex;
  align-items: center;
}
</style>
